package me.olook.netease.song.rank.rest;

import com.google.gson.Gson;
import me.olook.netease.song.rank.biz.WeiboUserService;
import me.olook.netease.song.rank.entity.WeiboUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Optional;

/**
 * @author zhaohw
 * @date 2018-05-24 10:53
 */
@RestController
public class WeiboUserRest {

    @Autowired
    private WeiboUserService weiboUserService;

    @GetMapping("/user/{id}")
    public Mono<String> sayHelloWorld(@PathVariable(value = "id") String id) {
        Optional<WeiboUser> weiboUser = weiboUserService.findById(id);
        return weiboUser.map(m -> Mono.just(new Gson().toJson(weiboUser))).orElse(null);
    }

    @GetMapping(value = "user/page")
    public Page<WeiboUser> getEntryByPageable(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                              @RequestParam(value = "size", defaultValue = "15") Integer size) {
        return weiboUserService.findByPage( PageRequest.of(page,size));
    }

    @GetMapping(value = "user/count")
    public Integer count() {
        return weiboUserService.countAll().intValue();
    }

    @GetMapping(value = "keyword/{name}/top")
    public Page<WeiboUser> getKeywordFansTopUser(@PathVariable(value = "name") String name,
                                                 @RequestParam(value = "page", defaultValue = "0") Integer page,
                                                 @RequestParam(value = "size", defaultValue = "15") Integer size) {
        return weiboUserService.findFansTopUsers(name,page,size);
    }

    @GetMapping(value = "keyword/all")
    public List<String> getKeywordAll() {
        return weiboUserService.findDistinctByAddBy();
    }

    @GetMapping(value = "keyword/{name}/gender")
    public  String getKeywordGender(@PathVariable(value = "name") String name) {
        return new Gson().toJson(weiboUserService.countAddByAndGender(name));
    }

    @GetMapping(value = "keyword/{name}/followers")
    public  String getKeywordFansSegment(@PathVariable(value = "name") String name) {
        return new Gson().toJson(weiboUserService.countFollowersSegment(name));
    }

    @GetMapping(value = "keyword/{name}/recent")
    public  String findAllByCrtTimeAfter(@PathVariable(value = "name") String name) {
        return new Gson().toJson(weiboUserService.findAllByCrtTimeAfter(name));
    }
}
